<?php
	class PaymentsController extends PaidListingsAppController
	{
		var $name = "Payments";
		var $uses = array("PaidListings.PaymentHandler","PaidListings.ListingPlan","Listing",'PaidListings.Order');
		//@todo use orderId (maybe)
		function offline($orderId)
		{
			$this->Order->recursive = 0;
			$order = $this->Order->findById($orderId);
			if(!$order)
			{
				$this->errorMsg("Invalid Order");
				$this->redirect("/");
			}
			$listingPlanId = $order['Order']['listing_plan_id'];
			$listingPlan = $this->ListingPlan->getListingPlanInfo($order['Order']['listing_plan_id']);
			$this->checkOwner($listingPlanId);
			$Handler = $this->PaymentHandler->getPaymentHandlerObject("Offline");
			if(is_object($Handler) && $Handler->isEnabled())
			{
				if($this->data)
				{
					$this->Order->setAsIncomplete($orderId);
					$this->successMsg(__("Your plan has been notified for payment."));
					$this->redirect('/users/profile');
				}
				$this->set("listingPlan",$listingPlan);
				$this->set("Handler",$Handler);
				$this->render();
				return;
			}
			$this->redirect("/");
		}
		
		function paypal($orderId)
		{
			$order = $this->Order->findById($orderId);
			$this->checkOwner($order['Order']['listing_plan_id']);
			$listingPlan = $this->ListingPlan->getListingPlanInfo($order['Order']['listing_plan_id']);
			$paymentHandlers = $this->PaymentHandler->getPaymentHandlers();
			if(isset($paymentHandlers['Paypal']) && $paymentHandlers['Paypal']->isEnabled())
			{
				$Paypal = $paymentHandlers['Paypal'];
				$ipnVars = $Paypal->getWebStandardPaymentInformation($order,$listingPlan);
				$ipnVars['notify_url'] = Router::url(array("action"=>'paypalIpn',$orderId),true);
				$ipnVars['return'] = Router::url(array("action"=>'paypalReturn',$orderId),true);
				$this->Order->setAsProcessing($orderId);
				$this->set("ipnVars",$ipnVars);
				$this->set("paypalUrl",$Paypal->getPaypalUrl());
			}else
			{
				$this->redirect('/');
			}
		}
		function paypalReturn($orderId)
		{
			$order = $this->Order->findById($orderId);
			$this->checkOwner($order['Order']['listing_plan_id']);
			$listingPlan = $this->ListingPlan->getListingPlanInfo($order['Order']['listing_plan_id']);
			$this->successMsg(__("Your payment has been completed."));
			$this->redirect('/');
		}
		function paypalIpn($orderId)
		{
			$Paypal = $this->PaymentHandler->getPaymentHandlerObject('Paypal');
			$order = $this->Order->findById($orderId);
			$this->log($_POST,"ipn_call/order_".$orderId);
			if($Paypal->validateIpn($_POST) && $order)
			{
				$listingPlan = $this->ListingPlan->getListingPlanInfo($order['Order']['listing_plan_id']);
				$paymentSrc = array('send_money','virtual_terminal','web_accept','express_checkout');
				//integrating only subscr_payment for now.
				if(($_POST['txn_type'] == "subscr_payment" && $order['Order']['payment_type'] == Plan::PAYMENT_SUBSCRIPTION) || in_array($_POST['txn_type'],$paymentSrc) )
				{
					if($Paypal->validateOrder($_POST,$order,$listingPlan))
					{
						$this->Order->setAsCompleted($orderId);
						$this->Order->Transaction->createTransaction($Paypal,$order,Transaction::SUCCESSFUL,$_POST);
					}else
					{
						$this->Order->setAsFailed($orderId);
						$this->Order->Transaction->createTransaction($Paypal,$order,Transaction::FAILED,$_POST);
					}
				}
			}else
				{
					$this->log($_POST,"invalid_ipn");
				}
			$this->_stop();
		}
		
		protected function checkOwner($listingPlanId)
		{
			$listingPlan = $this->ListingPlan->getListingPlanInfo($listingPlanId);
			$ownerId = $listingPlan['ListingPlan']['user_id'];
			if(!$ownerId || $this->Session->read("Auth.User.id") != $ownerId)
			{
                $this->errorMsg(__("Invalid URL."));
				$this->redirect('/');
			}
		}
	}
?>